#include <QTimer>
#include <QFileDialog>
#include "selfinfowidget.h"
#include "debug.h"
#include "model/data.h"
#include "model/datacenter.h"
#include "toast.h"


SelfinfoWidget::SelfinfoWidget(QWidget* parent)
    :QDialog(parent)
{
    //1.设置窗口属性
    this->setFixedSize(500/SCALE,250/SCALE);
    this->setWindowTitle("我的信息");
    this->setWindowIcon(QIcon(":/myResource/Image/WeChat.svg"));
    //关闭窗口时，自动销毁对象
    this->setAttribute(Qt::WA_DeleteOnClose);
    //把窗口移动到当前鼠标位置
    this->move(QCursor::pos());

    //2.设置布局管理器
    layout = new QGridLayout;
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);

    //3.设置窗口中的控件

    //设置头像
    avatarBtn = new QPushButton;
    avatarBtn->setFixedSize(100/SCALE,100/SCALE);
    avatarBtn->setIconSize(QSize(100/SCALE,100/SCALE));
    layout->addWidget(avatarBtn,0,0,3,1);

    //设置其他属性
    QString labelStyle = "QLabel{font-size:14px;font-weight:800;}";
    QString btnStyle="QPushButton{border:none;}QPushButton:pressed{background-color:rgb(210,210,210);}";
    QString editStyle="QLineEidt{}";
    int height = 40/SCALE;

    //用户ID
    idTag = new QLabel;
    idTag->setFixedSize(50,height);
    idTag->setText("用户ID:");
    idTag->setStyleSheet(labelStyle);
    idTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    idLabel = new QLabel;
    idLabel->setFixedHeight(height);
    idLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    //名字
    nameTag = new QLabel;
    nameTag->setFixedSize(50,height);
    nameTag->setText("姓名:");
    nameTag->setStyleSheet(labelStyle);
    nameTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    nameLabel = new QLabel;
    nameLabel->setFixedHeight(height);
    nameLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameModifyBtn = new QPushButton;
    nameModifyBtn->setFixedSize(75/SCALE,20);
    nameModifyBtn->setIcon(QIcon(":/myResource/Image/change.svg"));
    nameModifyBtn->setIconSize(QSize(20,20));
    nameModifyBtn->setStyleSheet(btnStyle);
    nameEdit = new QLineEdit;
    nameEdit->setStyleSheet(editStyle);
    nameEdit->setFixedHeight(height);
    nameEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameEdit->hide();
    nameSubmitBtn = new QPushButton;
    nameSubmitBtn->setFixedSize(75/SCALE,20);
    nameSubmitBtn->setIcon(QIcon(":/myResource/Image/commit.svg"));
    nameSubmitBtn->setIconSize(QSize(20,20));
    nameSubmitBtn->setStyleSheet(btnStyle);
    nameSubmitBtn->hide();

    //签名
    descTag = new QLabel;
    descTag->setFixedSize(50,height);
    descTag->setText("签名:");
    descTag->setStyleSheet(labelStyle);
    descTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    descLabel = new QLabel;
    descLabel->setFixedHeight(height);
    descLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    descModifyBtn = new QPushButton;
    descModifyBtn->setFixedSize(75/SCALE,20);
    descModifyBtn->setIcon(QIcon(":/myResource/Image/change.svg"));
    descModifyBtn->setIconSize(QSize(20,20));
    descModifyBtn->setStyleSheet(btnStyle);
    descEdit = new QLineEdit;
    descEdit->setStyleSheet(editStyle);
    descEdit->setFixedHeight(height);
    descEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    descEdit->hide();
    descSubmitBtn = new QPushButton;
    descSubmitBtn->setFixedSize(75/SCALE,20);
    descSubmitBtn->setIcon(QIcon(":/myResource/Image/commit.svg"));
    descSubmitBtn->setIconSize(QSize(20,20));
    descSubmitBtn->setStyleSheet(btnStyle);
    descSubmitBtn->hide();

    //手机号
    phoneTag = new QLabel;
    phoneTag->setFixedSize(50,height);
    phoneTag->setText("手机号:");
    phoneTag->setStyleSheet(labelStyle);
    phoneTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    phoneLabel = new QLabel;
    phoneLabel->setFixedHeight(height);
    phoneLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    phoneModifyBtn = new QPushButton;
    phoneModifyBtn->setFixedSize(75/SCALE,20);
    phoneModifyBtn->setIcon(QIcon(":/myResource/Image/change.svg"));
    phoneModifyBtn->setIconSize(QSize(20,20));
    phoneModifyBtn->setStyleSheet(btnStyle);
    phoneEdit = new QLineEdit;
    phoneEdit->setStyleSheet(editStyle);
    phoneEdit->setFixedHeight(height);
    phoneEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    phoneEdit->hide();
    phoneSubmitBtn = new QPushButton;
    phoneSubmitBtn->setFixedSize(75/SCALE,20);
    phoneSubmitBtn->setIcon(QIcon(":/myResource/Image/commit.svg"));
    phoneSubmitBtn->setIconSize(QSize(20,20));
    phoneSubmitBtn->setStyleSheet(btnStyle);
    phoneSubmitBtn->hide();

    //验证码
    verifyCodeTag = new QLabel;
    verifyCodeTag->setFixedSize(50,height);
    verifyCodeTag->setText("验证码:");
    verifyCodeTag->setStyleSheet(labelStyle);
    verifyCodeTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    verifyCodeTag->hide();
    verifyCodeEdit = new QLineEdit;
    verifyCodeEdit->setStyleSheet(editStyle);
    verifyCodeEdit->setFixedHeight(height);
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    verifyCodeEdit->hide();
    getVerifyCodeBtn = new QPushButton;
    getVerifyCodeBtn->setText("获取验证码");
    getVerifyCodeBtn->setFixedSize(75/SCALE,20);
    getVerifyCodeBtn->setIconSize(QSize(20,20));
    getVerifyCodeBtn->setStyleSheet(btnStyle);
    getVerifyCodeBtn->hide();

    //4.添加到布局管理器中
    layout->addWidget(idTag,0,1);
    layout->addWidget(idLabel,0,2);

    layout->addWidget(nameTag,1,1);
    layout->addWidget(nameLabel,1,2);
    layout->addWidget(nameModifyBtn,1,3);

    layout->addWidget(descTag,2,1);
    layout->addWidget(descLabel,2,2);
    layout->addWidget(descModifyBtn,2,3);

    layout->addWidget(phoneTag,3,1);
    layout->addWidget(phoneLabel,3,2);
    layout->addWidget(phoneModifyBtn,3,3);

//测试代码展示效果
#if TEST_UI

    idLabel->setText("9515611324");
    nameLabel->setText("zzy");
    descLabel->setText("从今天开始认真敲代码");
    phoneLabel->setText("18612345678");
    avatarBtn->setIcon(QIcon(":/myResource/Image/myAvatar.jpg"));

#endif
    initSignalSlot();
    //加载个人信息
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    model::UserInfo* myself = dataCenter->getMyself();
    if(myself != nullptr)
    {
        avatarBtn->setIcon(myself->avatar);
        idLabel->setText(myself->userId);
        nameLabel->setText(myself->nickname);
        descLabel->setText(myself->description);
        phoneLabel->setText(myself->phone);
    }


}
void SelfinfoWidget::initSignalSlot()
{
    connect(nameModifyBtn,&QPushButton::clicked,this,[&]()
    {
        //隐藏当前label和modify,显示出edit和submitButton
        nameLabel->hide();
        nameModifyBtn->hide();
        layout->removeWidget(nameLabel);
        layout->removeWidget(nameModifyBtn);
        nameEdit->show();
        nameSubmitBtn->show();
        layout->addWidget(nameEdit,1,2);
        layout->addWidget(nameSubmitBtn,1,3);
        //为了更好的用户体验，可以把原本的信息放到编辑框内
        nameEdit->setText(nameLabel->text());
    });

    connect(descModifyBtn,&QPushButton::clicked,this,[&]()
    {
        //隐藏当前label和modify,显示出edit和submitButton
        descLabel->hide();
        descModifyBtn->hide();
        layout->removeWidget(descLabel);
        layout->removeWidget(descModifyBtn);

        descEdit->show();
        descSubmitBtn->show();
        layout->addWidget(descEdit,2,2);
        layout->addWidget(descSubmitBtn,2,3);
        //为了更好的用户体验，可以把原本的信息放到编辑框内
        descEdit->setText(descLabel->text());
    });

    connect(phoneModifyBtn,&QPushButton::clicked,this,[&]()
    {
        //隐藏当前label和modify,显示出edit和submitButton
        phoneLabel->hide();
        phoneModifyBtn->hide();
        layout->removeWidget(phoneLabel);
        layout->removeWidget(phoneModifyBtn);

        phoneEdit->show();
        phoneSubmitBtn->show();
        layout->addWidget(phoneEdit,3,2);
        layout->addWidget(phoneSubmitBtn,3,3);
        //为了更好的用户体验，可以把原本的信息放到编辑框内
        phoneEdit->setText(phoneLabel->text());

        //修改手机号需要额外检验验证码是否正确，添加验证码相关控件
        verifyCodeTag->show();
        verifyCodeEdit->show();
        getVerifyCodeBtn->show();
        layout->addWidget(verifyCodeTag,4,1);
        layout->addWidget(verifyCodeEdit,4,2);
        layout->addWidget(getVerifyCodeBtn,4,3);
    });

    connect(nameSubmitBtn,&QPushButton::clicked,this,&SelfinfoWidget::clickNameSubmitBtn);
    connect(descSubmitBtn,&QPushButton::clicked,this,&SelfinfoWidget::clickDescSubmitBtn);
    connect(getVerifyCodeBtn,&QPushButton::clicked,this,&SelfinfoWidget::clickGetVerifyCodeBtn);
    connect(phoneSubmitBtn,&QPushButton::clicked,this,&SelfinfoWidget::clickPhoneSubmitBtn);
    connect(avatarBtn,&QPushButton::clicked,this,&SelfinfoWidget::clickAvatarBtn);


}

void SelfinfoWidget::clickNameSubmitBtn()
{
    const QString& nickname = nameEdit->text();
    if(nickname.isEmpty())
        return;
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::changeNicknameDone,this,&SelfinfoWidget::clickNameSubmitBtnDone,Qt::UniqueConnection);
    dataCenter->changeNicknameAsync(nickname);

}

void SelfinfoWidget::clickNameSubmitBtnDone()
{
    nameLabel->setText(nameEdit->text());
    layout->removeWidget(nameEdit);
    layout->removeWidget(nameSubmitBtn);
    nameEdit->hide();
    nameSubmitBtn->hide();
    nameLabel->show();
    nameModifyBtn->show();
    layout->addWidget(nameLabel,1,2);
    layout->addWidget(nameModifyBtn,1,3);
}

void SelfinfoWidget::clickDescSubmitBtn()
{
    const QString& desc =descEdit->text();
    if(desc.isEmpty())
        return;

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::changeDescDone,this,&SelfinfoWidget::clickDescSubmitBtnDone);
    dataCenter->changeDescAsync(desc);
}

void SelfinfoWidget::clickDescSubmitBtnDone()
{
    descLabel->setText(descEdit->text());
    layout->removeWidget(descEdit);
    layout->removeWidget(descSubmitBtn);
    descEdit->hide();
    descSubmitBtn->hide();
    descLabel->show();
    descModifyBtn->show();
    layout->addWidget(descLabel,2,2);
    layout->addWidget(descModifyBtn,2,3);
}

void SelfinfoWidget::clickGetVerifyCodeBtn()
{
    const QString& phone = phoneEdit->text();
    if(phone.isEmpty())
        return;
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->getVertifyCodeAsync(phone);

    getVerifyCodeBtn->setEnabled(false);
    getVerifyCodeBtn->setText("60s");
    waitTime = 60;
    QTimer* timer = new QTimer;
    connect(timer,&QTimer::timeout,this,[=]()
    {
        if(waitTime<=1)
        {
            timer->stop();
            timer->deleteLater();
            getVerifyCodeBtn->setText("获取验证码");
            getVerifyCodeBtn->setEnabled(true);
            return;
        }
        getVerifyCodeBtn->setText(QString::number(--waitTime)+"s");
    });
    timer->start(1000);
}

void SelfinfoWidget::clickPhoneSubmitBtn()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    const QString& verifyCodeId = dataCenter->getCurrentVerifyCodeId();
    if(verifyCodeId.isEmpty())
    {
        Toast::showMessage("服务器尚未返回响应，请稍后再试");
        return;
    }

    const QString& verifyCode = verifyCodeEdit->text();
    verifyCodeEdit->setText("");
    if(verifyCode.isEmpty())
    {
        Toast::showMessage("验证码不能为空");
        return;
    }

    connect(dataCenter,&model::DataCenter::changePhoneDone,this,&SelfinfoWidget::clickPhoneSubmitBtnDone,Qt::UniqueConnection);
    dataCenter->changePhoneAsync(dataCenter->getPhoneToChange(),verifyCodeId,verifyCode);
}

void SelfinfoWidget::clickPhoneSubmitBtnDone()
{
    waitTime = 1;
    //已经使用这个验证码实现功能后就置空verifyCodeId
    model::DataCenter::getInstance()->setCurrentVerifyCodeId("");
    phoneLabel->setText(phoneEdit->text());
    layout->removeWidget(phoneEdit);
    layout->removeWidget(phoneSubmitBtn);
    layout->removeWidget(verifyCodeEdit);
    layout->removeWidget(verifyCodeTag);
    layout->removeWidget(getVerifyCodeBtn);
    phoneEdit->hide();
    phoneSubmitBtn->hide();
    verifyCodeEdit->hide();
    verifyCodeTag->hide();
    getVerifyCodeBtn->hide();
    phoneLabel->show();
    phoneModifyBtn->show();
    layout->addWidget(phoneLabel,3,2);
    layout->addWidget(phoneModifyBtn,3,3);
}

void SelfinfoWidget::clickAvatarBtn()
{
    //1.弹出对话框，选择文件
    QString filter = "Image Files (*.png *.jpg *.jpeg *.svg)";
    QString imagePath = QFileDialog::getOpenFileName(this,"选择头像",QDir::homePath(),filter);
    if(imagePath.isEmpty())
    {
        LOG()<<"用户没有选择任何头像";
        return;
    }

    //2.根据路径获取文件内容
    const QByteArray& avatarData = model::loadFileToByteArray(imagePath);
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::changeAvatarDone,this,&SelfinfoWidget::clickAvatarBtnDone);
    dataCenter->changeAvatarAsync(avatarData);

}

void SelfinfoWidget::clickAvatarBtnDone()
{
    avatarBtn->setIcon(model::DataCenter::getInstance()->getMyself()->avatar);
}





















